﻿using Arch.EntityFrameworkCore.UnitOfWork;
using AutoMapper;
using Common.Lib.Dto;
using Common.Lib.Parameter;
using Common.Lib.Service;
using Server.Todo.Context;

namespace Server.Todo.Service
{
    public class LoginService : ILoginService
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly IMapper mapper;
        private readonly IRepository<User> repository;

        public LoginService(IUnitOfWork unitOfWork, IMapper mapper)
        {
            this.unitOfWork = unitOfWork;
            this.mapper = mapper;
            this.repository = unitOfWork.GetRepository<User>();
        }

        public async Task<ApiResponse> AddAsync(User model)
        {
            throw new NotImplementedException();
        }

        public async Task<ApiResponse> AddAsync(UserDto model)
        {
            throw new NotImplementedException();
        }

        public async Task<ApiResponse> DeleteAsync(int id)
        {
            throw new NotImplementedException();
        }

        public async Task<ApiResponse> GetAllAsync(QueryParameter queryParameter)
        {
            throw new NotImplementedException();
        }

        public async Task<ApiResponse> GetSingleAsync(int id)
        {
            throw new NotImplementedException();
        }

        public async Task<ApiResponse> LoginAsync(string account, string password)
        {
            try
            {
                var user = await unitOfWork.GetRepository<User>().GetFirstOrDefaultAsync(predicate:p=>p.Account.ToLower().Equals(account.ToLower()) && p.PassWord.Equals(password));
                if(user == null)
                {
                    return new ApiResponse($"账号或密码错误，请重试");
                }
                return new ApiResponse(true,user);
            }
            catch (Exception ex)
            {
                return new ApiResponse($"添加数据失败:{ex.Message}");
            }
        }

        public async Task<ApiResponse> RegisterAsync(UserDto dto)
        {
            try
            {
                var model = mapper.Map<User>(dto);
                var user = await repository.GetFirstOrDefaultAsync(predicate: p=>p.Account.ToLower() == model.Account.ToLower());
                if(user != null)
                {
                    return new ApiResponse($"账户 {model.Account} 已存在，请重试！");
                }

                model.CreateTime = DateTime.Now;
                var v = await repository.InsertAsync(model);
                
                
                if (await unitOfWork.SaveChangesAsync() > 0)
                {
                    return new ApiResponse(true, model);
                }
                return new ApiResponse("添加账户失败");
            }
            catch (Exception ex)
            {
                return new ApiResponse($"添加账户失败:{ex.Message}");
            }
        }

        public Task<ApiResponse> UpdateAsync(UserDto model)
        {
            throw new NotImplementedException();
        }
    }

    public interface ILoginService:IBaseService<UserDto>
    {
        Task<ApiResponse> LoginAsync(string account,string password);

        Task<ApiResponse> RegisterAsync(UserDto model);
    }
}
